<template>
  <view class="forget">
    <u-navbar
      title="重置密码"
      bgColor="transparent"
      :fixed="false"
      :autoBack="true"
      leftIconColor="#fff"
    >
      <template #center>
        <text style="color: #fff"> 重置密码 </text>
      </template>
    </u-navbar>
    <view class="content">
      <!-- 头部logo -->
      <!-- 主体 -->
      <view class="main">
        <!-- <view class="tips" style="margin-left: 0"
          >若你忘记了密码，可在此重置新密码。</view
        > -->
        <!-- <wInput
					v-model="nameData"
					type="text"
					maxlength="20"
					placeholder="请输入用户名"
				></wInput> -->
        <image
          src="/static/login/ava.png"
          style="width: 141rpx; height: 150rpx; border-radius: 50%"
        ></image>
        <wInput
          v-model="mailData"
          type="text"
          class="w-input"
          maxlength="20"
          placeholder="请输入手机号"
        ></wInput>
        <wInput
          v-model="passData"
          type="password"
          class="w-input"
          maxlength="20"
          placeholder="请输入新密码"
          isShowPass
        ></wInput>

        <wInput
          v-model="verCode"
          type="number"
          maxlength="6"
          placeholder="请输入验证码"
          class="w-input"
          isShowCode
          codeText="获取验证码"
          setTime="60"
          ref="runCode"
          @setCode="getVerCode()"
        ></wInput>
      </view>

      <wButton
        class="wbutton"
        text="重置密码"
        :rotate="isRotate"
        @click.native="startRePass()"
        fontColor="#fff"
        bgColor="linear-gradient(270deg, #4DCBA5 0%, #6BD6B5 100%)"
      ></wButton>
    </view>
  </view>
</template>

<script>
let _this;
import wInput from "../../components/watch-login/watch-input.vue"; //input
import wButton from "../../components/watch-login/watch-button.vue"; //button
import w_md5 from "../../js_sdk/zww-md5/w_md5.js";
export default {
  data() {
    return {
      // logoImage: 'http://q1.qlogo.cn/g?b=qq&nk=1660875559&s=100',
      logoImage: "/static/logo.png",
      nameData: "", //电话
      mailData: "", //电话
      passData: "", //密码
      verCode: "", //验证码
      isRotate: false, //是否加载旋转
      app_key: "",
      userInfo: {},
    };
  },
  components: {
    wInput,
    wButton,
    w_md5,
  },
  mounted() {
    _this = this;
    // this.get_userinfo()
  },
  methods: {
    getVerCode() {
      //获取验证码

      if (!uni.$u.test.email(this.mailData)) {
        uni.showToast({
          icon: "none",
          position: "bottom",
          title: "邮箱不正确",
        });
        return false;
      }
      var time = new Date().getTime();
      this.app_key = getApp().globalData.websiteKey;
      var param =
        "deviceid=" +
        getApp().globalData.deviceid +
        "&email=" +
        this.mailData +
        "&facility=" +
        getApp().globalData.facility +
        "&timestamp=" +
        time;
      var sign = w_md5.hex_md5_32Upper(param + this.app_key);
      uni.request({
        url:
          getApp().globalData.websiteUrl +
          "/index.php/appv1/login/telcode/pass?" +
          param +
          "&sign=" +
          sign,
        success: (res) => {
          console.log("邮件发送结果: ", res);
          if (res.data.code == 1) {
            this.$refs.runCode.$emit("runCode"); //触发倒计时（一般用于请求成功验证码后调用）
            uni.showToast({
              icon: "none",
              position: "bottom",
              title: res.data.msg,
            });
          } else {
            uni.showToast({
              icon: "none",
              position: "bottom",
              title: res.data.msg,
            });
          }
        },
      });
      console.log("获取验证码");
    },
    startRePass() {
      //重置密码
      if (this.isRotate) {
        //判断是否加载中，避免重复点击请求
        return false;
      }
      if (!uni.$u.test.email(this.mailData)) {
        uni.showToast({
          icon: "none",
          position: "bottom",
          title: "邮箱不正确",
        });
        return false;
      }
      if (this.verCode == "") {
        uni.showToast({
          icon: "none",
          position: "bottom",
          title: "请输入验证码",
        });
        return false;
      }
      if (this.passData == "") {
        uni.showToast({
          icon: "none",
          position: "bottom",
          title: "请输入新密码邮箱",
        });
        return false;
      }
      _this.isRotate = true;

      var time = new Date().getTime();
      this.app_key = getApp().globalData.websiteKey;
      var param =
        "code=" +
        this.verCode +
        "&deviceid=" +
        getApp().globalData.deviceid +
        "&email=" +
        this.mailData +
        "&facility=" +
        getApp().globalData.facility +
        "&pass=" +
        this.passData +
        "&timestamp=" +
        time;
      var sign = w_md5.hex_md5_32Upper(param + this.app_key);
      uni.request({
        url:
          getApp().globalData.websiteUrl +
          "/index.php/appv1/login/pass?" +
          param +
          "&sign=" +
          sign,
        success: (res) => {
          console.log("找回密码结果: ", res.data);
          if (res.data.code == 1) {
            _this.isRotate = false;
            uni.showToast({
              icon: "none",
              position: "bottom",
              title: res.data.msg,
            });
            uni.removeStorageSync("user");
            uni.navigateBack();
            console.log("重置密码成功");
          } else {
            _this.isRotate = false;
            uni.showToast({
              icon: "none",
              position: "bottom",
              title: res.data.msg,
            });
          }
        },
      });
    },
    // 获取用户信息
    get_userinfo() {
      var time = new Date().getTime();
      this.app_key = getApp().globalData.websiteKey;

      var param =
        "deviceid=" +
        getApp().globalData.deviceid +
        "&facility=" +
        getApp().globalData.facility +
        "&timestamp=" +
        time +
        "&user_id=" +
        uni.getStorageSync("user").uid +
        "&user_token=" +
        uni.getStorageSync("user").utoken;

      var sign = w_md5.hex_md5_32Upper(param + this.app_key);
      uni.request({
        url:
          getApp().globalData.websiteUrl +
          "/index.php/appv1/user/index?" +
          param +
          "&sign=" +
          sign,
        success: (res) => {
          console.log("用户信息：", res.data);
          this.userInfo = res.data.user;
        },
      });
    },
  },
};
</script>

<style>
@import url("../../components/watch-login/css/icon.css");
@import url("./css/main.css");
.forget {
  min-height: 100vh;
  background: linear-gradient(360deg, #6bd6b5 0%, #2ab1a4 100%);
}
.content {
  background-color: #fff;
  width: calc(100% - 48rpx);
  margin: auto;
  border-radius: 16rpx;
  padding-bottom: 64rpx;
  margin-top: 57rpx;
}
.w-input {
  background: #f0f8f5;
  border-radius: 44rpx;
  width: 100%;
}
.main {
  padding: 0 39rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-top: 63rpx;
}
</style>
